<html>
<head><meta charset="utf-8"><title>Issue #5610 · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html">Issue #5610</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216713477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216713477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Punit <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216713477">(Nov 14 2020 at 07:36)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust-clippy/issues/5610">https://github.com/rust-lang/rust-clippy/issues/5610</a></p>
<p>I understand that there are two things to be done here. </p>
<ol>
<li>Check if the expression type is &amp;str/String</li>
<li>Check if the method being called is to_string()</li>
</ol>
<p>I implemented 2 using the common tools doc. I tried to implement 1, using the solution provided by <span class="user-mention" data-user-id="264664">@flip1995</span>  (<code> if is_type_diagnostic_item(cx, arg_ty, sym!(string_type)) { </code>), but that doesn't work. Test fails even if i write <code>3.to_string();</code></p>
<p>Then i tried this but, here tests always pass. I found the issue to be that the if let ExprKind::MethodCall line is failing. I don't understand why</p>
<div class="codehilite"><pre><span></span><code>let ty = cx.typeck_results().expr_ty(expr);
        if let ty::Ref(_, ty, ..) = ty.kind() {
            if *ty.kind() == ty::Str {
                if_chain! {
                    if let ExprKind::MethodCall(path, _, _args, _) = &amp;expr.kind;
                    if path.ident.name == sym!(to_string);
                    then {
                        span_lint_and_help(
                            cx,
                            STR_TO_STRING,
                            expr.span,
                            &quot;`to_string()` does a multitude of things to just clone a string&quot;,
                            None,
                            &quot;consider using `.to_owned()`&quot;,
                        );
                    }
                }
            }
        }```
</code></pre></div>



<a name="216720112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216720112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216720112">(Nov 14 2020 at 10:08)</a>:</h4>
<p><code>3.to_string()</code> shouldn't lint, because 3 is not a string/str. </p>
<p>I'm not quite sure, why this doesn't work. Mainly because I don't have the context, where <code>ty</code>, <code>expr</code>, ... are coming from. Have you pushed some code, I can look at? If you open a WIP PR and <code>r? @flip1995</code> I can take a closer look.</p>



<a name="216731264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216731264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Punit <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216731264">(Nov 14 2020 at 14:15)</a>:</h4>
<p>I have opened a WIP PR. After rebasing, the code is not compiling. its showing errors in other files<br>
<a href="/user_uploads/4715/Pc6TQuq-7WR0Q0tOY2wvU0WH/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/Pc6TQuq-7WR0Q0tOY2wvU0WH/image.png" title="image.png"><img src="/user_uploads/4715/Pc6TQuq-7WR0Q0tOY2wvU0WH/image.png"></a></div>



<a name="216731346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216731346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216731346">(Nov 14 2020 at 14:17)</a>:</h4>
<p>You probably have to rerun the <a href="http://setup-toolchain.sh">setup-toolchain.sh</a> script</p>



<a name="216782323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216782323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ThibsG <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216782323">(Nov 15 2020 at 09:42)</a>:</h4>
<p>hey <span class="user-mention" data-user-id="354252">@Punit</span> , just made some comments on your PR, don't be scared, it's almost only NITs there <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span> <br>
did you manage to compile and test your code finally?</p>



<a name="216787571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216787571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Punit <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216787571">(Nov 15 2020 at 11:43)</a>:</h4>
<p>Yes, i was able to compile the code after re-running the setup toolchain. Thanks for the suggestions, I will make the changes that you have suggested</p>



<a name="216791151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216791151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Punit <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216791151">(Nov 15 2020 at 12:55)</a>:</h4>
<p>Something seems to be up. Tests are compiling fine on my machine. But the checks on PR are failing</p>



<a name="216791553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216791553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216791553">(Nov 15 2020 at 13:04)</a>:</h4>
<p>That's because something in rustc changed. I'll fix it right away.</p>



<a name="216791772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/216791772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#216791772">(Nov 15 2020 at 13:11)</a>:</h4>
<p>After <a href="https://github.com/rust-lang/rust-clippy/issues/6334">clippy#6334</a> is merged, you can rebase and your PR should be green. (Locally you have to rerun setup-toolchain once again for you local test-pass)</p>



<a name="217718264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/217718264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Punit <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#217718264">(Nov 24 2020 at 06:23)</a>:</h4>
<p>Running tests/ui/update-all-references.sh, adds this to a few stderr files. Should i go ahead and commit this?<br>
<a href="/user_uploads/4715/rRyonRP91_Dd504jVYt6k9Ik/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/rRyonRP91_Dd504jVYt6k9Ik/image.png" title="image.png"><img src="/user_uploads/4715/rRyonRP91_Dd504jVYt6k9Ik/image.png"></a></div>



<a name="217719174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/217719174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Punit <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#217719174">(Nov 24 2020 at 06:44)</a>:</h4>
<p>Also for merging the lints to a single string module, I'll have to change it so that it has only a single <code>declare_lint_pass!</code> macro, right? So that it can have a single <code>impl LateLintPass</code></p>



<a name="217773023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/217773023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#217773023">(Nov 24 2020 at 15:57)</a>:</h4>
<p>Nope, this requires a rustup. I will do one immediately.</p>



<a name="217774807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Issue%20%235610/near/217774807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Issue.20.235610.html#217774807">(Nov 24 2020 at 16:08)</a>:</h4>
<p><span class="user-mention" data-user-id="354252">@Punit</span> After <a href="https://github.com/rust-lang/rust-clippy/pull/6374">https://github.com/rust-lang/rust-clippy/pull/6374</a> is merged you can rebase on top of master and everything should pass again.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>